package com.cc2019.buke.home.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.cc2019.buke.MainActivity;
import com.cc2019.buke.R;
import com.cc2019.buke.home.entry.Cart;
import com.cc2019.buke.home.entry.Product2;
import com.cc2019.buke.home.view.AddAndReduce;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;
import com.ejlchina.okhttps.OkHttps;
public class ProRxActivity extends AppCompatActivity {

    private Product2 product;
    private SliderLayout sliderLayout;
    //    @BindView(R.id.proinfo_tv1)
    TextView proinfo_tv1;
    //    @BindView(R.id.proinfo_tv2)
    TextView proinfo_tv2;
    //    @BindView(R.id.proinfo_pname)
    TextView proinfo_pname;
    private TextView proinfo_add_price;
    private TextView proinfo_add_car;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_rx);

        proinfo_tv1 = findViewById(R.id.proinfo_tv12);
        proinfo_tv2 = findViewById(R.id.proinfo_tv22);
        proinfo_pname = findViewById(R.id.proinfo_pname2);
        proinfo_add_car = findViewById(R.id.proinfo_add_car2);
        proinfo_add_price = findViewById(R.id.proinfo_dd_price2);
        //当前界面，要显示的商品信息
        Intent intent = this.getIntent();
        product = (Product2)intent.getSerializableExtra("product");
        sliderLayout = this.findViewById(R.id.proinfo_slider2);
        //初始化页面
        initView();
        //初始化轮播图界面
        getSomebanner();

        //添加购物车点击事件
        proinfo_add_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCartDialog();
            }
        });
    }
    public void showCartDialog(){
        View view1 = View.inflate(this,R.layout.proinfo_buycar_dialog,null);
        AlertDialog alertDialog = new AlertDialog.Builder(this).setView(view1).show();

        Window dialogWindow = alertDialog.getWindow();
        // 设置对话框的 Gravity 位置
        dialogWindow.setGravity(Gravity.BOTTOM);
        // 设置对话框的位置（这里的 x, y 位置是相对于 Gravity 的位置），大小和透明度
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = 600;  //布局文件中的高度不起作用，在此处设置
        dialogWindow.setAttributes(layoutParams);
        dialogWindow.getDecorView().setBackgroundColor(Color.WHITE);

        // 设置对话框的显示和隐藏动画（参见 res/values/styles.xml 中的 MyAlertDialogAnimationStyle）
        dialogWindow.setWindowAnimations(R.style.proinfo_dialog_animation);
        //初始化弹框中的内容
        ImageView imageView = view1.findViewById(R.id.buy_car_img);
        TextView proNameView = view1.findViewById(R.id.buy_car_proName);
        TextView priceView = view1.findViewById(R.id.buy_car_price);
        //商品图片
        String img = product.getImg();
        //轮播图第一张
        String imgUrl = "http://124.221.67.36:9998/android/upload/"+img+".jpg";
        Glide.with(view1).load(imgUrl).into(imageView);

        proNameView.setText(product.getName());
        priceView.setText("$:"+product.getPrice());
        Button button1 = view1.findViewById(R.id.buy_car_dismiss);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        //
        Button button2 = view1.findViewById(R.id.buy_car_ok);
//        button2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //判断用户是否登录
//                SharedPreferences sp = ProRxActivity.this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
//                if (sp!=null && !sp.getString("name","").equals("")){
//                    int uid = sp.getInt("id",-1);
//                    double price = product.getPrice();
//                    String pname = product.getName();
//                    int pid = product.getId();
//                    AddAndReduce addAndReduce = view1.findViewById(R.id.buy_caraddordel);
//                    int total = addAndReduce.getTotal();
//                    Cart cart = new Cart(1,uid,pid,pname,price,total);
//                    //发送post请求
//                    OkHttps.async("http://10.0.2.2:8080/add_Cart")
//                            .bind(ProRxActivity.this)
//                            .setBodyPara(cart)
//                            .setOnResString(str ->{
//                                System.out.println(str);
//                            }).post();
//                }else {
//                    Intent intent = new Intent(ProRxActivity.this, MainActivity.class);
//                    intent.setAction("toUserInfo");
//                    ProRxActivity.this.startActivity(intent);
//
//                }
//
//                //发送请求到数据库，取出，看是否有值
//
//                //跳转到登录界面
//
//
//            }
//        });

        //设置界面上显示的商品图片、名称、价格、及取消和确定按钮事件
//        ImageView imageView = view1.findViewById(R.id.buy_car_img);
//        Glide.with(view1).load("http://124.221.67.36:9998/android/upload/"+product.getImg()+".jpg").into(imageView);
//
//        TextView buy_car_proName = view1.findViewById(R.id.buy_car_proName);
//        buy_car_proName.setText(product.getName());
//
//        TextView buy_car_price = view1.findViewById(R.id.buy_car_price);
//        buy_car_price.setText("¥: "+product.getPrice());

    }

    public void initView(){
        //初始化产品相关信息
        double price = product.getPrice();
        double oldPrice = product.getOldPrice();
        String name = product.getName();
        String img = product.getImg();
        String imgUrl = ("http://124.221.67.36:9998/android/upload/"+img+".jpg");
        initBanner(imgUrl);

        proinfo_tv1.setText("卷后"+price);
        proinfo_tv2.setText("卷后"+oldPrice);
        proinfo_pname.setText(name);
    }
    public void getSomebanner(){
        OkHttps.async("http://124.221.67.36:9998/android/upload/findSome").bind(this).setOnResList(Product2.class,pros ->{
            for (Product2 pro:pros){
                String imgName = pro.getImg();
                String imgUrl = ("http://124.221.67.36:9998/android/upload/"+imgName+".jpg");
                initBanner(imgUrl);
            }
        }).get();
    }

    public void initBanner(String imgUrl){
        DefaultSliderView defaultSliderView = new DefaultSliderView(this);
        defaultSliderView.image(imgUrl);
        sliderLayout.addSlider(defaultSliderView);
        //设置切换时间
        sliderLayout.setDuration(2000);
    }
}